package com.icss.servlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.icss.beans.StuClassVO;
import com.icss.daos.StuClassDao;

public class ClassManageServlet extends HttpServlet {

	public ClassManageServlet() {
		super();
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String action = request.getParameter("action");
		StuClassDao cdao = new StuClassDao();
		if("show".equals(action)){
			request.getRequestDispatcher("/page/office/class/showClass.jsp").forward(request, response);
		}else if("showClass".equals(action)){
			
			ArrayList<StuClassVO> list = cdao.getAllClass();
		
			for(int i=0;i<list.size();i++){
				StuClassVO c = list.get(i);			
				c.setCount(cdao.getClassCount(c.getStuClassId()));
				
			}
			
			
			request.setAttribute("classList", list);
			request.getRequestDispatcher("/page/office/class/manageClass.jsp").forward(request, response);
		}else if("addClass".equals(action)){
			String className= request.getParameter("stuClassName");
			String classTime = request.getParameter("classTime");
			StuClassVO scv = new StuClassVO();
			scv.setClassName(className);
			scv.setClassTime(classTime);
			cdao.addClass(scv);
			request.getRequestDispatcher("ClassManageServlet?action=showClass").forward(request, response);
		}else if("editClass".equals(action)){
			int classID = Integer.parseInt(request.getParameter("stuClassId"));
			StuClassVO scv = cdao.getOneClass(classID);
			request.setAttribute("stuClass", scv);
			request.getRequestDispatcher("/page/office/class/editClass.jsp").forward(request, response);
		}else if("updateClass".equals(action)){
			String className= request.getParameter("stuClassName");
			String classTime = request.getParameter("classTime");
			int classID = Integer.parseInt(request.getParameter("classID"));
			StuClassVO scv = new StuClassVO();
			scv.setClassName(className);
			scv.setClassTime(classTime);
			scv.setStuClassId(classID);
			cdao.updateClass(scv);
			request.getRequestDispatcher("ClassManageServlet?action=showClass").forward(request, response);
		}else if("delClass".equals(action)){
			int classID = Integer.parseInt(request.getParameter("stuClassId"));
			cdao.delClass(classID);
			request.getRequestDispatcher("ClassManageServlet?action=showClass").forward(request, response);
		}else if("delAllSelect".equals(action)){
			String arr = request.getParameter("uidList");
			cdao.delAllSelect(arr);
			request.getRequestDispatcher("ClassManageServlet?action=showClass").forward(request, response);
		}
		cdao.close();
	}

	public void init() throws ServletException {
		// Put your code here
	}

}
